<?php
class PhoneValidationCore extends ObjectModel
{
	public $id;
	public $id_customer;
	public $phone_mobile;
	public $code;
	public $ip_address;
	public $validation_count;
	public $state;
	public $date_add;
	public $date_upd;
	
	protected $table = 'phone_validation';
	protected $identifier = 'id_phone_validation';
	
	protected $fieldsRequired = array('id_customer','phone_mobile','code','ip_address');
	protected $fieldsSize = array('code' => 32);
	protected $fieldsValidate = array('id_customer' => 'isUnsignedId','phone_mobile' => 'isCellphone',  'ip_address' => 'isIp2Long');
	
	public	function getFields()
	{
		parent::validateFields();
		$fields['id_customer'] = (int)($this->id_customer);
		$fields['phone_mobile'] = pSQL($this->phone_mobile);
		$fields['code'] = pSQL($this->code);
		$fields['ip_address'] = (int)($this->ip_address);
		$fields['validation_count'] = (int)($this->validation_count);
		$fields['state'] = (int)($this->state);
		$fields['date_add'] = pSQL($this->date_add);
		$fields['date_upd'] = pSQL($this->date_upd);
		return $fields;
	}
	
	/**
	 * Validate the customer's phone code
	 *
	 * @param $id_customer integer 
	 * @param $phone_mobile string
	 * @param $code string
	 * @return array Corresponding customers
	 */
	public static function checkPhoneCode($id_customer, $phone_mobile, $code)
	{
		$result =  Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('SELECT id_customer FROM '._DB_PREFIX_.'phone_validation WHERE id_customer = '.(int)($id_customer).
				' and phone_mobile = '.pSQL($phone_mobile).
				' and code = '.pSQL($code));
		return isset($result['id_customer']);
	}
	
	
}